package business;

import java.util.List;

import persistence.LibraryLocation;
import persistence.LibraryLocationDAO;
import persistence.PredefinedEntry;
import persistence.PredefinedEntryDAO;
import persistence.PredefinedParam;
import persistence.PredefinedParamDAO;
import persistence.exception.DAOException;
import business.exception.BusinessException;

public class CommonLogicImpl implements CommonLogic {

	private LibraryLocationDAO locationDAO;
	private PredefinedParamDAO predefinedParamDAO;
	private PredefinedEntryDAO predefinedEntryDAO;

	@Override
	public void setLibraryLocationDAO(LibraryLocationDAO locationDAO) {
		this.locationDAO = locationDAO;		
	}
	public void setPredefinedParamDAO(PredefinedParamDAO predefinedParamDAO) {
		 this.predefinedParamDAO = predefinedParamDAO;
	}
	public void setPredefinedEntryDAO(PredefinedEntryDAO predefinedEntryDAO) {
		this.predefinedEntryDAO = predefinedEntryDAO;
	}
	
	@Override
	public List<LibraryLocation> getAllLibraryLocations() throws BusinessException {
		List<LibraryLocation> locations = null;
		try {
			locations = locationDAO.findAll();
		}
		catch (DAOException de) {
			throw new BusinessException("Problem when attempting to get all LibraryLocations", de);
		}
		return locations;
	}	
	@Override
	public List<PredefinedEntry> findEntriesForParam (String paramName) throws BusinessException {
		List<PredefinedEntry> entries = null;
		try {
			PredefinedParam currencyParam = findParamByName(paramName);
			entries = predefinedEntryDAO.findAllWithParameter(currencyParam);
		}
		catch (DAOException de) {
			throw new BusinessException("Problem when attempting to get all currencies", de);
		}
		return entries;
	}
	@Override
	public PredefinedParam findParamByName(String name) throws BusinessException {
		PredefinedParam param = null;
		try {
			param = predefinedParamDAO.findParamByName(name);
		}
		catch (DAOException de) 
		{
			throw new BusinessException("Problem when attempting to find PredefinedParam with name: " + name, de);
		}
		return param;
	}
}
